package com.sixth.controller;

import com.sixth.until.CommonResult;
import com.sixth.until.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * @program: self-tui-education
 * @description:
 * @author: 高振杨
 * @create: 2022-04-15 11:56
 **/
@RestController
@RequestMapping("/sso")
public class LoginoutController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @PostMapping("/login")
    public CommonResult loginout(){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        stringRedisTemplate.delete(token);
        return new CommonResult(2000,"退出成功",null);
    }

    //面试 它考虑得问题很多 比如并发 线程安全问题 效率问题等等。。。。。
    //工作 先完成任务----测试问题---优化你的任务。 需要的时间比较长1天。并不像我们在这里1个小时。
    @GetMapping("info")
    public CommonResult info(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();
        return new CommonResult(2000,"获取当前用户信息成功",principal);
    }


}
